package xuegao.study.mall.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xuegao.study.mall.common.model.pms.*;
import xuegao.study.mall.web.domain.PmsProductCategoryNode;
import xuegao.study.mall.web.domain.PmsProductDetail;
import xuegao.study.mall.web.service.PmsPortalProductService;
import xuegao.study.mall.repo.dao.pms.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PmsPortalProductServiceImpl implements PmsPortalProductService {
    @Autowired
    private PmsProductCategoryDao pmsProductCategoryDao;

    @Autowired
    private PmsProductDao pmsProductDao;

    @Autowired
    private PmsBrandDao pmsBrandDao;

    @Autowired
    private PmsProductAttributeDao pmsProductAttributeDao;

    @Autowired
    private PmsProductAttributeValueDao pmsProductAttributeValueDao;

    @Autowired
    private PmsSkuStockDao pmsSkuStockDao;

    @Autowired
    private PmsProductLadderDao pmsProductLadderDao;

    @Autowired
    private PmsProductFullReductionDao pmsProductFullReductionDao;

    @Override
    public List<PmsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize, Integer sort) {
        // todo 具体逻辑需要考虑一下
        PageHelper.startPage(pageNum, pageSize);
        List<PmsProduct> productList = pmsProductDao.selectByMap(new HashMap<>());
        return productList;
    }

    @Override
    public List<PmsProductCategoryNode> categoryTreeList() {
        Map<String, Object> params = new HashMap<>();
        List<PmsProductCategory> allList = pmsProductCategoryDao.selectByMap(params);
        List<PmsProductCategoryNode> result = allList.stream()
                .filter(item -> item.getParentId().equals(0L))
                .map(item -> convert(item, allList))
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public PmsProductDetail detail(Long id) {
        PmsProductDetail detail = new PmsProductDetail();
        // 获取商品信息
        PmsProduct pmsProduct = pmsProductDao.selectById(id);
        detail.setProduct(pmsProduct);
        // 获取品牌信息
        PmsBrand pmsBrand = pmsBrandDao.selectById(pmsProduct.getBrandId());
        detail.setBrand(pmsBrand);
        // 获取商品属性信息
        Long productAttributeCategoryId = pmsProduct.getProductAttributeCategoryId();
        Map<String, Object> params = new HashMap<>();
        params.put("product_attribute_category_id", productAttributeCategoryId);
        List<PmsProductAttribute> productAttributeList = pmsProductAttributeDao.selectByMap(params);
        detail.setProductAttributeList(productAttributeList);
        // 获取商品属性值信息
        if (!CollUtil.isEmpty(productAttributeList)) {
            List<Long> attributeIds = productAttributeList.stream().map(PmsProductAttribute::getId).collect(Collectors.toList());
            List<PmsProductAttributeValue> productAttributeValueList = pmsProductAttributeValueDao.selectByIds(attributeIds, pmsProduct.getId());
            detail.setProductAttributeValueList(productAttributeValueList);
        }
        // 获取商品sku库存信息
        Map<String, Object> skuQueryParams = new HashMap<>();
        skuQueryParams.put("product_id", pmsProduct.getId());
        List<PmsSkuStock> skuStockList = pmsSkuStockDao.selectByMap(skuQueryParams);
        detail.setSkuStockList(skuStockList);
        // 商品阶梯价格设置
        if (pmsProduct.getPromotionType() == 3) {
            Map<String, Object> promotionParams = new HashMap<>();
            promotionParams.put("product_id", pmsProduct.getId());
            List<PmsProductLadder> productLadderList = pmsProductLadderDao.selectByMap(promotionParams);
            detail.setProductLadderList(productLadderList);
        }
        // 商品满减价格设置
        if (pmsProduct.getPromotionType() == 4) {
            Map<String, Object> fullParams = new HashMap<>();
            fullParams.put("product_id", pmsProduct.getId());
            List<PmsProductFullReduction> productFullReductionList = pmsProductFullReductionDao.selectByMap(fullParams);
            detail.setProductFullReductionList(productFullReductionList);
        }
        // todo 商品可用优惠卷
        return detail;
    }

    private PmsProductCategoryNode convert(PmsProductCategory item, List<PmsProductCategory> allList) {
        PmsProductCategoryNode node = new PmsProductCategoryNode();
        BeanUtils.copyProperties(item, node);
        List<PmsProductCategoryNode> children = allList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getId()))
                .map(subItem -> convert(subItem, allList))
                .collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
